package telepasea.app;

import java.util.Calendar;
import java.util.StringTokenizer;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class PushAcitivty extends Activity{

	TextView text;
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
	
	
	  getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED  
	              | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD  
	              | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON  
	              | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

	  requestWindowFeature(Window.FEATURE_NO_TITLE);      
		    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		    getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND, 
		            WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
		
		    
		setContentView(R.layout.dialog);

		Intent intent = getIntent(); 
		String c2dm_msg = intent.getExtras().getString("msg");
		
		try{
			msgParser(c2dm_msg);
		}catch(Exception e){
			e.printStackTrace();
		}
	}


	private void msgParser(String c2dm_msg) throws Exception{
		StringTokenizer token = new StringTokenizer(c2dm_msg, "/");
		
		String name = token.nextToken();
		String sex = token.nextToken();
		String act = token.nextToken();
		final String phonenum = Security.decrypt(token.nextToken());
		
		System.out.println("Received Push Name/Sex/Act/PhoneNum======>" + name + "/" + sex + "/" + act + "/" + phonenum + "/");
		
		//Push Filtering
		Resources res = getApplicationContext().getResources();
		String[] itemlist = res.getStringArray(R.array.ActivityList);
		
		PushFilterUI myFilter = new PushFilterUI();
		
		String PF_ONOFF = Util.getSharedPreferencesValue(getApplicationContext(), "telepasea", Context.MODE_PRIVATE, "PF_ONOFF");
		if (!PF_ONOFF.equals("checked"))
			return;
		
		if (sex.equals("1")) {
			String PF_M = Util.getSharedPreferencesValue(getApplicationContext(), "telepasea", Context.MODE_PRIVATE, "PF_M");
			if (!PF_M.equals("checked"))
				return;
		}
		else if (sex.equals("2") ){
			String PF_F = Util.getSharedPreferencesValue(getApplicationContext(), "telepasea", Context.MODE_PRIVATE, "PF_F");
			if (!PF_F.equals("checked"))
				return;
		}
		else
			return;
		
		int i;
		for (i = 0; i < 5; i++){
			if (act.equals(itemlist[i])){
				String PF_ACT = Util.getSharedPreferencesValue(getApplicationContext(), "telepasea", Context.MODE_PRIVATE, "PF_ACT"+(i + 1));
				if (!PF_ACT.equals("checked"))
					return;
				break;
			}
		}
		
		if (i == 5)
			return;
		
		//Notify
		 
		Vibrator vibrator = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
		vibrator.vibrate(500);
		
		final Context context = PushAcitivty.this;
		Builder alt_bld2 = new AlertDialog.Builder(context);
		alt_bld2.setMessage(name + " : " + act).setCancelable(true)
			.setPositiveButton("CALL", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phonenum));
					context.startActivity(intent);
					PushAcitivty.this.finish();
				}
			}).setNeutralButton("SMS", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					Intent intent = new Intent(Intent.ACTION_VIEW);
					intent.putExtra("address", phonenum);
					intent.setType("vnd.android-dir/mms-sms");
					context.startActivity(intent);
					PushAcitivty.this.finish();
				}
			}).setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id) {
					PushAcitivty.this.finish();
				}
			});
  
		AlertDialog alert2 = alt_bld2.create();
		alert2.show();
		  

	}	
}
